package database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper
{

	private static final int DATABASE_VERSION = 1;
	 
    // Database Name
    private static final String DATABASE_NAME = "exerciseManager";
 
    //table name
    private static final String TABLE_EXERCISE = "exercises";
 
    //Table Columns names
    private static final String EX_NAME = "name";
    private static final String NUM_SETS = "sets";
    private static final String QTY = "qty";
    private static final String DESC = "desc";
	
	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//onUpgrade(db, 1, 1);
		String CREATE_EXERCISE_TABLE = "CREATE TABLE " + TABLE_EXERCISE + "("
                + EX_NAME + " TEXT PRIMARY KEY," + NUM_SETS + " INTEGER,"
                + QTY + " INTEGER," + DESC + " TEXT " + ")";
        db.execSQL(CREATE_EXERCISE_TABLE);
		//db.execSQL("CREATE TABLE exercises (name TEXT,sets INTEGER,qty INTEGER,desc TEXT)");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXERCISE);
		 
        // Create tables again
        onCreate(db);
	}
	
	// Adding new
    public void addExercise(Exercise exercise) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(EX_NAME, exercise.get_name()); 
        values.put(NUM_SETS, exercise.get_numofsets()); 
        values.put(QTY, exercise.get_qty()); 
        values.put(DESC, exercise.get_desc()); 
        
        // Inserting Row
        db.insert(TABLE_EXERCISE, null, values);
        db.close(); // Closing database connection
    }
    
    // Deleting single
    public void deleteExercise(Exercise exercise) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_EXERCISE, EX_NAME + " = ?",
                new String[] { String.valueOf(exercise.get_name()) });
        db.close();
    }
    
    public Exercise getExercise(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_EXERCISE, new String[] { EX_NAME,
                NUM_SETS, QTY,DESC }, EX_NAME + "=?",
                new String[] { String.valueOf(name) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Exercise ex = new Exercise(cursor.getString(0),
                Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)),
                cursor.getString(3));
        // return contact
        return ex;
    }

}
